<template>
    <div>
        <Card shadow>
            <iview-table
                ref="table"
                :baseURL="queryModel.baseURL"
                :menuId="dataModel.menuId"
                :columns="dataModel.columns"
                :order="dataModel.order"
                :pageNumber="pageModel.pageNumber"
                :pageSize="pageModel.pageSize"
                :defaultFilters="queryModel.defaultFilters"
                v-on="{changeTotalSize:changeTotalSize,events:events,changeSelect:changeSelect}">
            </iview-table>
            <br>
            <iview-page
                v-if="this.menuId !== -1"
                :totalSize="pageModel.totalSize"
                :pageNumber="pageModel.pageNumber" 
                :pageSize="pageModel.pageSize"
                :pageSizeOpts="pageModel.pageSizeOpts"
                v-on="{changePageNumber:changePageNumber,changePageSize:changePageSize}">
            </iview-page>
        </Card>
        <Modal v-model="showAdd" :closable='false' ok-text="保存" :mask-closable=true :width="500">
            <h3 slot="header" style="color:#2D8CF0">请填写配置信息</h3>
            <Row>
                <Col span="22" offset="1">
                    <Form :model="formItem" :label-width="100" label-position="right">
                        <FormItem label="季节名称：" prop="categoryId">
                            <Input v-model="formItem.seasonName" placeholder=""/>
                        </FormItem>
                        <FormItem label="季节日期：" prop="categoryId">
                            <DatePicker v-model="seasonDate" type="date" placeholder="" style="width: 100%" @on-change="changeDate"></DatePicker>
                        </FormItem>
                        <FormItem label="皮肤类型：" prop="categoryId" placeholder="">
                            <Select v-model="skinTypes" multiple style="width: 100%">
                                <Option value="O">油性(O)</Option>
                                <Option value="D">干性(D)</Option>
                                <Option value="S">敏感(S)</Option>
                                <Option value="R">耐受(R)</Option>
                                <Option value="P">色素(P)</Option>
                                <Option value="N">非色素(N)</Option>
                                <Option value="W">皱纹(W)</Option>
                                <Option value="T">紧致(T)</Option>
                            </Select>
                        </FormItem>
                    </Form>
                </Col>
            </Row>
            <div slot="footer">
                <Button type="info" size="large" @click="save">保存</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
import iviewTable from '@/view/common/table/Table.vue';
import iviewPage from '@/view/common/page/Page.vue';
// import Button from "../../common/button/Button";
export default {
    name: 'text-editor',
    components: {
        iviewTable,
        iviewPage
    },
    props: {
        menuId: {
            type: [Number, String],
            default: 340
        },
        checkDate: {
            type: Boolean,
            default: false
        }
    },
    data () {
        return {
            baseURL: '/user/season',
            showAdd: false,
            selectRows: [],
            queryModel: {
                // 网关代理路径(必须)+资源路径(可选)
                baseURL: '/user/season',
                defaultFilters: [
                ]
            },
            dataModel: {
                menuId: this.menuId,
                columns: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },{
                        title: '季节名称',
                        key: 'seasonName',
                    },{
                        title: '季节日期',
                        key: 'seasonDate',
                    },{
                        title: '提醒皮肤类型',
                        key: 'skinTypes',
                        render: (h, params) => {
                            let temp = params.row.skinTypes
                            temp = temp.replace('[', '').replace(']', '').replace('O', '油性').replace('D', '干性').replace('S', '敏感').replace('R', '耐受').replace('P', '色素').replace('N', '非色素').replace('W', '皱纹').replace('T', '紧致')
                            return h('span', temp)
                        }
                    }
                ],
                order: 'id desc'
            },
            pageModel: {
                totalSize: 0,
                pageNumber: 1,
                pageSize: 10,
                pageSizeOpts: [10, 50, 100, 500]
            },
            skinTypes: [],
            seasonDate: '',
            formItem: {
                id: '',
                seasonName: '',
                seasonDate: '',
                skinTypes: ''
            }
        };
    },
    methods: {
        events (item) {
            let path = item.path
            if (path === 'add') {
                this.add()
            } else if (path === 'edit') {
                this.edit()
            } else if (path === 'delete') {
                this.delete()
            }
        },
        add () {
            this.skinTypes = []
            this.seasonDate = ''
            this.$bus.init(this.formItem)
            this.showAdd = true
        },
        edit () {
            if (this.selectRows.length === 1) {
                this.formItem = this.selectRows[0]
                this.showAdd = true
            } else {
                this.$bus.alert('warning', '请选择一条记录')
            }
        },
        delete () {
            if (this.selectRows.length > 0) {
                let ids = []
                for (let row of this.selectRows) {
                    ids.push(row.id)
                }
                if (ids.length === 1) {
                    this.doDelete(ids)
                } else {
                    this.$Modal.confirm({
                        title: '操作提示',
                        content: '<p>确认删除'+ids.length+'条记录?</p>',
                        onOk: () => {
                            this.doDelete(ids)
                        }
                    })
                }
            } else {
                this.$bus.alert('warning', '请至少选择一条记录')
            }
        },
        doDelete (ids) {
            let url = this.baseURL + '/delete'
            this.$ajax.post(url, ids).then((response) => {
                let {code, msg} = response.data
                if (code === 0) {
                    this.$bus.alert('success', msg)
                    this.refresh()
                } else {
                    this.$bus.alert('error', msg)
                }
            })
        },
        changeDate (value, type) {
            this.formItem.seasonDate = value
        },
        save () {
            this.formItem.skinTypes = JSON.stringify(this.skinTypes)
            let url = this.baseURL + '/saveOrUpdate'
            this.$ajax.post(url, this.formItem).then(r => {
                let {code, msg} = r.data
                if (code === 0) {
                    this.$bus.alert('success', '保存成功')
                    this.showAdd = false
                    this.refresh()
                } else {
                    this.$bus.alert('error', msg)
                }
            })
        },
        refresh () {
            if (this.checkDate) {
                this.queryModel.defaultFilters[0] = {'field': 'season_date', 'value1': this.$moment(new Date()).format('YYYY-MM-DD'), 'operate': '=='}
            }
            this.$refs.table.refresh()
        },
        changeSelect (rows) {
            this.selectRows = rows
        },
        changeTotalSize (totalSize) {
            this.pageModel.totalSize = totalSize
        },
        changePageNumber (pageNumber) {
            this.pageModel.pageNumber = pageNumber
        },
        changePageSize (pageSize) {
            this.pageModel.pageSize = pageSize
        },
        init () {
        }
    },
    mounted () {
        this.refresh()
    },
    watch: {
        '$route' (to) {
            if (to.path === '/season') {
                this.refresh()
            }
        }
    }
}
</script>

<style>

</style>
